/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.message.controller;

import cn.icanci.basic.commonutils.R;
import cn.icanci.message.client.BaseDataServiceClient;
import cn.icanci.message.service.SendSmsService;
import cn.icanci.message.utils.RandomValueUtils;
import cn.icanci.message.utils.RegexPatterns;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.client.naming.utils.StringUtils;

/**
 * @author icanci
 * @date Created in 2021/04/10 16:17
 * @version 1.0
 */
@SuppressWarnings("all")
@RestController
@Slf4j
@RequestMapping("/message")
public class AliyunSMSMessageController {
    @Resource
    private SendSmsService        sendSmsService;
    @Resource
    private BaseDataServiceClient baseDataServiceClient;

    @GetMapping("/getSmsCode/{phone}")
    public R getMessageCode(@PathVariable("phone") String phone) {
        // 验证手机号
        String regexPhone = RegexPatterns.REGEX_PHONE;
        Pattern compilePhone = Pattern.compile(regexPhone);
        Matcher matcherPhone = compilePhone.matcher(phone);
        boolean matchesPhone = matcherPhone.matches();
        if (!matchesPhone) {
            return R.error().message("请输入正确的手机号码");
        }
        // 调用方法发送
        String code = String.valueOf(baseDataServiceClient.getCode(phone));
        if (!StringUtils.isEmpty(code) && !"null".equals(code)) {
            log.warn("验证码已经存在，请查看你的手机,{}", code);
            return R.error().message("验证码已经存在，请查看你的手机").data("code", code);
        }
        // 生成6位数验证码并且存储到Redis中
        code = String.valueOf(RandomValueUtils.getNum(100000, 999999));
        HashMap<String, Object> codeMap = new HashMap<>();
        codeMap.put("code", code);
        log.info("aliyun send message,{}", code);
        boolean isSend = sendSmsService.send(phone, "SMS_196653273", codeMap);
        if (isSend) {
            log.info("短信已经发送，5分钟之内有效，请注意查收,phone:{},code:{}", phone, code);
            // 将手机号作为key,验证码作为 code，有效时间是5分钟
            baseDataServiceClient.setCode(phone, code, 30 * 60);
            return R.ok().message("短信已经发送，5分钟之内有效，请注意查收").data(codeMap);
        } else {
            log.error("网络异常，发送失败,phone:{},code:{}", phone, code);
            return R.error().message("网络异常，发送失败");
        }
    }
}
